package com.qianduo.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.qianduo.dao.IAccountDao;
import com.qianduo.po.QianduoAccount;
import com.qianduo.po.QianduoAccountView;
import com.qianduo.util.Constants;
import com.qianduo.util.ServiceUtil;
import com.qianduo.vo.AccountForm;

import common.page.PageList;
import common.util.ObjectUtil;
import common.util.StringUtil;

@SuppressWarnings("finally")
public class AccountServiceImpl extends ServiceUtil {
	private IAccountDao accountDao;	
	
	public IAccountDao getAccountDao() {
		return accountDao;
	}
	public void setAccountDao(IAccountDao accountDao) {
		this.accountDao = accountDao;
	}
	public boolean saveOrUpdateAccount(AccountForm vo){
		boolean ret	= false;
		try{
			QianduoAccount po	= new QianduoAccount();
			ObjectUtil.copyProperties(po, vo);
			accountDao.saveOrUpdateAccount(po);		
			ret	= true;
		}catch(Exception e){
			Constants.log.error(className+" saveOrUpdateAccount",e);
		}finally{
			return ret;
		}
	}
	public boolean UpdateAccountUncludePassword(AccountForm vo){
		boolean ret	= false;
		try{
			QianduoAccountView po	= accountDao.getQianduoAccount(vo.getUsername());
			vo.setPassword(po.getPassword());
			
			ret	= saveOrUpdateAccount(vo);
		}catch(Exception e){
			Constants.log.error(className+" UpdateAccountUncludePassword",e);
		}finally{
			return ret;
		}
	}
	
	public AccountForm getQianduoAccount(String username){
		AccountForm ret	= null;
		if(StringUtil.isEmpty(username)){
			return ret;
		}
		try{
			QianduoAccountView po	= accountDao.getQianduoAccount(username);
			if(null != po){
				ret	= new AccountForm();
				ObjectUtil.copyProperties(ret, po);
			}
		}catch(Exception e){
			Constants.log.error(className+" getQianduoAccount",e);
		}finally{
			return ret;
		}
	}
	public AccountForm getQianduoAccount(String username, String password){
		AccountForm ret	= null;
		if(StringUtil.isEmpty(username) || StringUtil.isEmpty(password)){
			return ret;
		}
		try{
			QianduoAccountView po	= accountDao.getQianduoAccount(username, password);
			if(null != po){
				ret	= new AccountForm();
				ObjectUtil.copyProperties(ret, po);
			}
		}catch(Exception e){
			Constants.log.error(className+" getQianduoAccount",e);
		}finally{
			return ret;
		}
	}
	
	public List<AccountForm> listAccount(String username,Integer accountType,Integer agencyID,String agencyName,String ext,String phone,Integer isVerify,Integer isCert,Integer isBusinessLicense,Integer recommendStar,Integer cityID,String companyName,String productType,String address,PageList pageList){
		List<AccountForm> retList	= null;
		try{
			List<QianduoAccountView> listPO	= accountDao.listAccount(username, accountType, agencyID, agencyName, ext, phone, isVerify, isCert, isBusinessLicense, recommendStar, cityID, companyName, productType, address, pageList);
			if(null != listPO && listPO.size() > 0){
				retList	= new ArrayList<AccountForm>();
				for(QianduoAccountView po : listPO){
					AccountForm vo	= new AccountForm();
					ObjectUtil.copyProperties(vo,po);
					
					retList.add(vo);
				}
			}
		}catch(Exception e){
			Constants.log.error(className+" listAccount",e);
		}finally{
			return retList;
		}
	}
}
